<?php
	class ControllerMemoryTest extends Controller {
		private $error = array();
		public function index(){
			$url = "http://www.w3school.com.cn/jsref/dom_obj_anchor.asp";
			$htmlinfo = file_get_contents($url);
			$htmlinfo = iconv("gb2312", "utf-8//IGNORE",$htmlinfo);
			$this->cache->set("htmlcontent",$htmlinfo);
			$this->document->setTitle("test");
			$data['maincontent'] = "测试专用页面";
			if($this->request->server['REQUEST_METHOD'] == 'POST') {
				// $this->load->model('knowledge/knowledge');
				//$this->load->model('settingreview/settingreview');
				$this->load->model('setting/setting');
				// $this->load->model('complate/complate');
				// $this->load->model('test/test');
				// $this->load->model('user/user');
				// $this->load->model('message/message');
				// $today = date("Y-m-d");//"2017-03-01";//
				// $todaystarttime = $today." 00:00:01";
				// $todayendtime = $today." 23:59:59";
				try{
					$filter_data = array(
						'user_id'   => $this->session->data['user_id'],
						'label'     => 'dasssndan'
					);
					$results = $this->model_setting_setting->editDefaultLabel($filter_data);
					header('Content-Type:application/json; charset=utf-8');
					exit(json_encode($results,0));
				}
				catch(Exception $e){
					exit(json_encode($e->getMessage(),0));
				}
			}
			
			//$data['contents'] = $htmlinfo;
			$data['header'] = $this->load->controller('common/headerin');
	 		$data['footerin'] = $this->load->controller('common/footerin');
	 		$data['footer'] = $this->load->controller('common/footer');
	 		$data['addurl'] = $this->url->link('memory/test/addKnowledgeData', '',true)."&token=".$this->session->data['token'];
	 		$data['delurl'] = $this->url->link('memory/test/delKnowledgeData', '',true)."&token=".$this->session->data['token'];
	 		$data['selfurl'] = $this->url->link('memory/test', '',true)."&token=".$this->session->data['token'];
	 		$this->response->setOutput($this->load->view('memory/test', $data));
		}
		public function addKnowledgeData(){
			if(($this->request->server['REQUEST_METHOD'] == 'POST')) {
				$this->load->model('knowledge/knowledge');
				$this->load->model('settingreview/settingreview');
				$this->load->model('complate/complate');
				preg_match_all('|title="(.*)">(.*)<|iUs', $this->cache->get("htmlcontent"), $matchs);
				$str3 = var_export($matchs,TRUE);
				file_put_contents("system\storage\arraypreg.php", $str3,FILE_APPEND);
				foreach ($matchs[2] as $key => $value) {
					foreach ($matchs[1] as $k => $v) {
						if($key == $k){
							$info['kname'] = $value;
							$info['kdetail'] = $v;
							$info['klabel'] = $key.",".$value.",".$k.",".$v;
							$info['ftime'] = date('Y-m-d H:i');
							$info['user_id']  = $this->session->data['user_id']; 
							if($kid=$this->model_knowledge_knowledge->addKnowledge($info)){
							$query = $this->model_settingreview_settingreview->getSettingreview($this->session->data['user_id']);
							if(empty($query)){
								$query = $this->config->get("setting_review");
							}
							$ftime = $this->model_knowledge_knowledge->getFirsttimebykid($kid);
							//调用complatetime类来决定复习时间
							$this->load->library("complatetime");
							$c_time = new complatetime($ftime,$query);
							$complatetime = $c_time->getComplatetime();
						};
						$result = $this->model_complate_complate->addComplate($complatetime,$kid,$this->session->data['user_id']);
						}
					}
				}
				header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($result,0));
			}
		}
		public function delKnowledgeData(){
			if(($this->request->server['REQUEST_METHOD'] == 'POST')) {
				$this->load->model('knowledge/knowledge');
				$this->load->model('complate/complate');
				if(!$this->model_complate_complate->cleanComplate()&&!$this->model_knowledge_knowledge->cleanKnowledge()){
					$result="已清空所有知识点！";
				};
				header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($result,0));
			}
		}
		public function FindFile($dir) { 
			 $files = array();
			 if($handle = opendir($dir)){   //打开目录
			  	while(($file = readdir($handle)) !== false){  //读取文件名
			    	if($file !="." && $file !=".."){    //排除上级目录 和当前目录
			     		if(is_dir($dir."\\".$file)){    //如果还是文件夹  继续遍历
			                $files[$file] = scandir($dir . "/" . $file);
			     		}else {
			                     $files[] = $file;
			            }      
			   		}
			  	}
			    closedir($handle);
				return $files;
			} 
		}
	}
	//$dir = "E:\\files\\website\\s\\admin\\view\\image\\loading";
	//			$files = $this->FindFile($dir);